package graphics.base;

/**
 * 底层转化帮助类
 * 
 * @author ephuizi@gmial.com
 *
 */
public class BaseUtil {
	public static final String STL_FILE_HEAD = "STL File - Created BY PROGRAM With: Image to STL Converter By ephuizi";

	/**
	 * 将int转成低位先的字节组
	 * 
	 * @param v
	 * @param target
	 */
	public static void toBytesLowFirst(int v, byte[] target) {
		target[0] = (byte) ((v >>> 0) & 0xFF);
		target[1] = (byte) ((v >>> 8) & 0xFF);
		target[2] = (byte) ((v >>> 16) & 0xFF);
		target[3] = (byte) ((v >>> 24) & 0xFF);
	}

	/**
	 * 将float转成低位先的字节组
	 * 
	 * @param v
	 * @param target
	 */
	public static void toBytesLowFirst(float v, byte[] target) {
		int v1 = Float.floatToIntBits(v);

		target[0] = (byte) ((v1 >>> 0) & 0xFF);
		target[1] = (byte) ((v1 >>> 8) & 0xFF);
		target[2] = (byte) ((v1 >>> 16) & 0xFF);
		target[3] = (byte) ((v1 >>> 24) & 0xFF);
	}

	/**
	 * 将Triangle转成低位地位字节组
	 * 
	 * @param tri
	 * @param lowBytes
	 */
	public static void triangleToLowBytes(Triangle tri, byte[] lowBytes) {
		byte[] temp = new byte[4];
		toBytesLowFirst(tri.nx, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i] = temp[i];
		}
		toBytesLowFirst(tri.ny, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4] = temp[i];
		}
		toBytesLowFirst(tri.ny, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 2] = temp[i];
		}
		// 1
		toBytesLowFirst(tri.x1, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 3] = temp[i];
		}
		toBytesLowFirst(tri.y1, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 4] = temp[i];
		}
		toBytesLowFirst(tri.z1, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 5] = temp[i];
		}
		// 2
		toBytesLowFirst(tri.x2, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 6] = temp[i];
		}
		toBytesLowFirst(tri.y2, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 7] = temp[i];
		}
		toBytesLowFirst(tri.z2, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 8] = temp[i];
		}
		// 3
		toBytesLowFirst(tri.x3, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 9] = temp[i];
		}
		toBytesLowFirst(tri.y3, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 10] = temp[i];
		}
		toBytesLowFirst(tri.z3, temp);
		for (int i = 0; i < temp.length; i++) {
			lowBytes[i + 4 * 11] = temp[i];
		}
	}

	/**
	 * 字节转换为浮点 利用&补位
	 * 
	 * @param lowFirstBytes
	 *            字节（至少4个字节）
	 * @param
	 * @return
	 */
	public static float toFloat(byte[] lowFirstBytes) {
		return toFloat(lowFirstBytes, 0);
	}

	/**
	 * 字节转换为浮点 利用&补位
	 * 
	 * @param lowFirstBytes
	 *            字节（至少4个字节）
	 * @param
	 * @return
	 */
	public static float toFloat(byte[] lowFirstBytes, int start) {
		int l = (lowFirstBytes[start] & 0xFF) | (lowFirstBytes[start + 1] << 8 & 0xFFFF)
				| (lowFirstBytes[start + 2] << 16 & 0xFFFFFF) | (lowFirstBytes[start + 3] << 24 & 0xFFFFFFFF);

		return Float.intBitsToFloat(l);
	}
}
